<template>
  <el-container>
    <el-header>
      <h3>发放记录</h3>
    </el-header>
    <el-main>
      <el-form ref="form" :inline="true" :model="form">
        <el-form-item prop="query_field">
          <el-input v-model="phone" type="text" suffix-icon="el-icon-search" placeholder="请输入手机号码" style="width: 200px" @keyup.enter.native="search" />
          <el-input v-model="name" type="text" suffix-icon="el-icon-search" placeholder="请输入姓名" style="width: 200px" @keyup.enter.native="search" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click.prevent="search">查询</el-button>
          <el-button type="primary" icon="el-icon-refresh" @click="handleReset">重置</el-button>
          <el-button type="primary" style="float:right;" class="iconfont iconicon_add" @click.prevent="add">添加订单</el-button>
        </el-form-item>
      </el-form>
      <el-divider />
      <el-table
        v-loading="loading"
        :data="tableData"
        border
      >
        <el-table-column
          label="手机号"
          prop="phone"
          align="center"
        />
        <el-table-column
          label="姓名"
          prop="name"
          align="center"
        />
        <el-table-column
          label="单位"
          prop="company"
          align="center"
        />
        <el-table-column
          label="学习卡次数"
          prop="study_card_num"
          align="center"
        />
        <el-table-column
          label="到期时间"
          prop="end_date"
          align="center"
        />
        <el-table-column
          label="发放时间"
          prop="create_time"
          align="center"
        />
        <el-table-column
          label="订单状态"
          align="center"
        >
          <template slot-scope="scope">
            <el-tooltip v-if="scope.row.status == 1" class="item" effect="dark" content="当前手机号未被注册,学习卡未同步!" placement="top">
              <el-link style="color:blue;" :underline="false">{{ scope.row.status_name }}</el-link>
            </el-tooltip>
            <el-tooltip v-if="scope.row.status == 2" class="item" effect="dark" content="当前手机号已被注册，学习卡已经同步到用户卡包中!" placement="top">
              <el-link style="color:green;" :underline="false">{{ scope.row.status_name }}</el-link>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column
          label="兑换时间"
          prop="update_time"
          align="center"
        />
        <el-table-column
          label="操作"
          align="center"
        >
          <template slot-scope="scope">
            <el-button v-if="scope.row.status == 1" type="danger" size="small" icon="el-icon-delete" @click.prevent="del(scope.row.id)">删除</el-button>
            <el-button v-if="scope.row.status == 2" type="danger" disabled size="small" icon="el-icon-delete">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-main>
    <el-footer>
      <el-pagination
        background
        layout="prev, pager, next, total"
        :total="total"
        :current-page="page"
        :page-size="pageSize"
        @current-change="changePage"
      />
    </el-footer>
  </el-container>
</template>

<script>
import { orderList, delRecord } from '@/api/order'
export default {
  name: 'List',
  inject: ['reload'],
  data() {
    return {
      form: {
        query_field: ''
      },
      tableData: [],
      page: 1,
      total: 0,
      pageSize: 10,
      loading: true,
      name: '',
      phone: ''
    }
  },
  mounted() {
    orderList({}).then(response => {
      this.tableData = response.data.list
      this.loading = false
      this.page = response.data.paginate.current
      this.pageSize = response.data.paginate.pageSize
      this.total = response.data.paginate.total
    }).catch(err => {
      console.log(err)
    })
  },
  methods: {
    reset(ref) {
      this.$refs[ref].resetFields()
    },
    search() {
      var params = {}
      params.phone = this.phone
      params.name = this.name
      this.loading = true
      orderList(params).then(response => {
        this.tableData = response.data.list
        this.page = Number(response.data.paginate.current)
        this.pageSize = Number(response.data.paginate.pageSize)
        this.total = Number(response.data.paginate.total)
        this.loading = false
      }).catch(err => {
        this.loading = false
      })
    },
    changePage(currentPage) {
      var params = {
        page: currentPage,
        pageSize: this.pageSize
      }
      if (this.form.query_field.trim().length != 0) {
        params.phone = this.form.query_field
      }
      this.loading = true
      orderList(params).then(response => {
        this.tableData = response.data.list
        this.page = Number(response.data.paginate.current)
        this.pageSize = Number(response.data.paginate.pageSize)
        this.total = Number(response.data.paginate.total)
        this.loading = false
      }).catch(err => {
        this.loading = false
      })
    },
    del(order_id) {
      this.$confirm('学习卡分发记录一旦删除,就无法恢复', '删除操作', {
        confirmButtonText: '删除',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var data = {
          record_id: order_id
        }
        delRecord(data).then(response => {
          this.$message.success(response.msg)
          this.reload()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    add() {
      this.$router.push({ path: '/order-offline/add' })
    },
    handleReset() {
      Object.assign(this.$data, this.$options.data())
      orderList({}).then(response => {
        this.tableData = response.data.list
        this.loading = false
        this.page = response.data.paginate.current
        this.pageSize = response.data.paginate.pageSize
        this.total = response.data.paginate.total
      }).catch(err => {
        console.log(err)
      })
    }
  }
}
</script>

<style scoped>
  .el-container {
    width: 99%;
    max-width: 100%;
    overflow: hidden;
    border: 1px solid #F2F6FC;
    border-radius: 4px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);
    margin: 10px auto;
  }
  .el-header {
    text-align: center;
  }
  .el-header h3 {
    font-size: 20px;
    line-height: 20px;
    height: 20px;
  }

  .el-footer{
    text-align: center;
  }
</style>
